﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class exchange_input : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] == null)
        {
            Response.Redirect("error_log.aspx", true);
        }
        if (Session["IDENTITY"].Equals("employee") == true)
        {
            Response.Redirect("error_auth.aspx", true);
        }
        string name = Session["USERNAME"].ToString();
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
        SqlCommand com_get = new SqlCommand();
        com_get.Connection = sc;
        com_get.CommandText = "select PRODUCT_ID,PRODUCT_NAME,CREDITS_RATE,PRODUCT_SORT,PRODUCT_INTRO,PRODUCT_IMAGE from INFO_PRODUCT where ENTER_ID=@eid1";
        com_get.Parameters.AddWithValue("@eid1", Session["ENTERID"].ToString());
        SqlDataReader reader = com_get.ExecuteReader();
        GridView1.DataSource = reader;
        //ImageField newi = new ImageField();

        //GridView1.Columns.Add(newi);
        GridView1.DataBind();

        sc.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //先验证tb1和tb2是否为空
        if (TextBox1.Text == "" || TextBox1.Text == null || TextBox2.Text == "" || TextBox2.Text == null || TextBox3.Text == "" || TextBox3.Text == null)
        {
            Page.RegisterStartupScript("ss", "<script>alert('请输入正确的信息！')</script>");
            return;
        }
        else
        {
            //先验证输入的产品ID是否存在且是否属于登录用户，若属于继续
            string name = Session["USERNAME"].ToString();
            SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc1.Open();
            SqlCommand com_check = new SqlCommand();
            com_check.Connection = sc1;
            com_check.CommandText = "select PRODUCT_NAME from INFO_PRODUCT where PRODUCT_ID=@pid and ENTER_ID=@eid2";
            com_check.Parameters.AddWithValue("@pid",TextBox1.Text);
            com_check.Parameters.AddWithValue("@eid2",Session["ENTERID"].ToString());
            object a = com_check.ExecuteScalar();
            if (a == null)
            {
                sc1.Close();
                Page.RegisterStartupScript("ss", "<script>alert('产品ID有误，请重新填写！')</script>");
                return;
            }
            else
            {//验证INFO_EXCHANGE表中是否已存在该产品信息，防止重复录入
                SqlCommand com_check1 = new SqlCommand();
                com_check1.Connection = sc1;
                com_check1.CommandText = "select EX_RATE from INFO_EXCHANGE where PRODUCT_ID=@pid";
                com_check1.Parameters.AddWithValue("@pid",TextBox1.Text);
                object aa = com_check1.ExecuteScalar();
                if (aa == null)
                {// 存入INFO_EXCHANGE表产品ID和兑换比例
                    SqlCommand com_check2 = new SqlCommand();
                    com_check2.CommandText = "select count(INFO_EXCHANGE.PRODUCT_ID) from INFO_EXCHANGE,INFO_PRODUCT where INFO_EXCHANGE.PRODUCT_ID=INFO_PRODUCT.PRODUCT_ID and INFO_PRODUCT.ENTER_ID=@eid3";
                    com_check2.Connection = sc1;
                    com_check2.Parameters.AddWithValue("@eid3",Session["ENTERID"].ToString());
                    int count_hg=Convert.ToInt32(com_check2.ExecuteScalar().ToString());
                    if (count_hg <= 6)//换购产品上限数
                    {
                        try
                        {
                            SqlCommand com_insert = new SqlCommand();
                            com_insert.Connection = sc1;
                            com_insert.CommandText = "insert into INFO_EXCHANGE values (@pid,@rt,@sum)";
                            com_insert.Parameters.AddWithValue("@pid", TextBox1.Text);
                            com_insert.Parameters.AddWithValue("@rt", TextBox2.Text);
                            com_insert.Parameters.AddWithValue("@sum", TextBox3.Text);
                            while (Application["sqllock"] == "lock")
                            {//检测是否已被锁住
                                System.Threading.Thread.Sleep(2000);
                            }
                            Application["sqllock"] = "lock";//上锁                                   
                            if (com_insert.ExecuteNonQuery() == 1)
                            {
                                Application["sqllock"] = "unlock";//解锁
                                sc1.Close();
                                Page.RegisterStartupScript("ss", "<script>alert('兑换商品发布成功！')</script>");
                                return;
                            }
                            else
                            {
                                Application["sqllock"] = "unlock";//解锁
                                sc1.Close();
                                Page.RegisterStartupScript("ss", "<script>alert('数据库异常，请重试！')</script>");
                                return;
                            }

                        }
                        catch (SqlException ee)
                        {
                            sc1.Close();
                            Page.RegisterStartupScript("ss", "<script>alert('请确认您输入的兑换积分值和数量为整数！" + ee.Message + ee.StackTrace + "')</script>");
                            return;
                        }
                    }
                    else
                    {
                        sc1.Close();
                        Page.RegisterStartupScript("ss", "<script>alert('您发布的换购产品数量已达上限6个，请撤销已有换购商品后重试！')</script>");
                        return;
                    }
                }
                else
                {
                    sc1.Close();
                    Page.RegisterStartupScript("ss", "<script>alert('该产品已经发布过并且尚未下架，请下架后重新发布！')</script>");
                    return;
                }

            }
            
        }
    }
}